local rule = fk.CreateSkill {
    name = "#ling_1v2_mode",
  }
  

local T = require "packages/lingling_extra/record/Theorem"
local white_mode={"2v2_mode","1v2_mode","role_mode"}
local firstblood={}
-- hufu_save  hufu 
  --游戏结束时开始结算宝珠
rule:addEffect(fk.GameFinished, {
  priority = 0.001,
  global = true,
    can_refresh = function (self, event, target, player, data)
      local room=player.room
        local ai=table.find(room.players,function(p)
          return p.id<0
        end)
        local iswhitemode=false
        for _, mode in ipairs(white_mode) do
          if room:isGameMode(mode) then
            iswhitemode=true
            break
          end
        end
        return not ai and (not room:getBanner("has_get")) and iswhitemode
        --不开启人机时开始结算
    end,
    on_refresh = function (self, event, target, player, data)
      local room=player.room
      room:setBanner("has_get",1)
      local point=room:getBanner("@jbs1v2_mode_POINT") or 1
      local win = data:split("+")
      local winners = table.filter(room.players, function(p)
        return table.contains(win, p.role) and p.id>0
      end)
      local losers=table.filter(room.players,function(p)
        return (not table.contains(win,p.role)) and p.id>0
      end)
      --胜者加分

      for _,p in ipairs(winners) do
        local get=1*point
        if p.role=="lord" then--地主积分翻倍
          get=get*2
          T.changeplayerhufu(room,p,get)
        else
          --两个农民都存活则+1分
          if #winners==2 then
            get=get+1
          end
          T.changeplayerhufu(room,p,get)
        end
        if not table.contains(firstblood,p.id) then
          table.insert(firstblood,p.id)
          T.changeplayerhufu(room,p,20)

        end
      end
      --败者扣分
      for _,p in ipairs(losers) do
        local get=1*point
        local hufu=T.getplayerhufu(room,p)
        if hufu<=0 then
          p:doNotify("ShowToast", Fk:translate("#jbs_money_less"))
        else
          if p.role=="lord" then--地主积分翻倍
            get=get*2
            T.changeplayerhufu(room,p,-get)
          else
            T.changeplayerhufu(room,p,-get)
          end
        end
      end
      --结束

    end,
})


rule:addEffect(fk.GameStart, {
  priority = 0.001,
  global = true,
  can_refresh = function(self, event, target, player, data)
    return target==player
  end,
  on_refresh = function(self, event, target, player, data)
    local room=player.room
    for _,p in ipairs(room.players) do
      T.getplayerhufu(room,p)
    end
  end,

})


Fk:loadTranslationTable{
  ["#jbs_money_less"]="你已经破产！",
}

return rule